﻿/*
--------------------------------------
File: WeightedColorNoise.cs
Project: Themaopdracht 7
Author: Maarten Kroon

Description:
Adds color noise to the image, based on a number between: -amount and +(amount + 1)
--------------------------------------
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;

namespace THO7_Team8
{
    class WeightedColorNoise : VisionAlgorithm
    {
        public WeightedColorNoise(String name) : base(name) {
            hasConfiguration = true;
            configuration = new WeightedNoiseConfiguration();
        }
        public override System.Drawing.Bitmap DoAlgorithm(System.Drawing.Bitmap sourceImage)
        {
            Bitmap returnImage = new Bitmap(sourceImage);
            int[] rgbValues = MyImage.BmpToArray(returnImage);
            //The Amount noise to add, this should be a value from the GUI
            int Amount = _configValue;
            Random TempRandom = new Random();

            //Modified pixelArray            
            int[] rgbValues2 = new int[rgbValues.Length];

            int width = sourceImage.Width;
            int height = sourceImage.Height;

            //Loops through all the pixels in the array
            for (int i = 0; i < width * height; i++)
            {
                //Adds a number between -amount and +(amount + 1) to all colors of the pixel
                int blue = rgbValues[i] & 255 + TempRandom.Next(-Amount, Amount + 1);
                int green = rgbValues[i] >> 8 & 255 + TempRandom.Next(-Amount, Amount + 1);
                int red = rgbValues[i] >> 16 & 255 + TempRandom.Next(-Amount, Amount + 1);

                //If the color is > 255 makes it 255
                blue = blue > 255 ? 255 : blue;
                blue = blue < 0 ? 0 : blue;
                green = green > 255 ? 255 : green;
                green = green < 0 ? 0 : green;
                red = red > 255 ? 255 : red;
                red = red < 0 ? 0 : red;
                rgbValues2[i] = 255 << 24 | (Convert.ToByte(red) << 16) | (Convert.ToByte(green) << 8) | Convert.ToByte(blue);
            }
            //Returns the modified image
            returnImage = MyImage.ArrayToBmp(rgbValues2, returnImage);
            return returnImage;
        }
    }
}






